package com.yehuishou.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yehuishou.pojo.AssessmentValue;
import com.yehuishou.pojo.BackAssessment;
import com.yehuishou.service.AssessmentValueService;
import com.yehuishou.service.BackAssessmentService;
import com.yehuishou.service.BrandService;
import com.yehuishou.service.ClassifyService;
import com.yehuishou.service.ModelService;

/**
 * 
 * @author xiaosa
 * @className AssessmentController.java
 * @descripe 评估操作的控制器
 * @data 2018年6月5日
 */

@Controller
public class AssessmentController {
	
	
	Map<String, Object> map = new HashMap<>();
	
	@Autowired
	private BackAssessmentService backAssessmentService;
	
	@Autowired

	private BrandService brandService;

	@Autowired

	private ClassifyService classifyService;

	@Autowired

	private ModelService modelService;
	
	@Autowired
	
	private AssessmentValueService assessmentValueService;
	
	
	@RequestMapping("/content-2_1")
	public String toAssessIndex(HttpServletRequest req) {
		
		req.setAttribute("classifylist", classifyService.getAllClassify());
		req.setAttribute("assesslist",backAssessmentService.getBackAssessment( classifyService.getAllClassify().get(0).getCid()));

		return "assessvalue";
	}
	
	//根据型号id 获取评估条件列表 
	@ResponseBody
	@RequestMapping("/getassessmentBymid")
	public Map<String, Object> getassessmentBymid(Integer cid){
		Map<String, Object>  map = new HashMap<>();
		
		map.put("assessment", backAssessmentService.getBackAssessment(cid));
		return map;
	}
	
	//验证要添加的评估条件是否已经存在
	@ResponseBody
	@RequestMapping("/checkAssessExist")
	public Map<String, Object> checkAssessExist(BackAssessment back,HttpServletRequest req){
		
		//获取cid 查找商品所对应的属性值
		
		Map<String, Object>  map = new HashMap<>();
		//若存在则返回true 否则返回false
		map.put("flag",backAssessmentService.checkAssessExist(back));
		return map;
		
	}
	
	//根据商品型号获取商品的评估条件
	@ResponseBody
	@RequestMapping("/addAssessment")
	public Map<String, Object> addAssessment(BackAssessment back) {
		System.out.println(back);
		Map<String, Object>  map = new HashMap<>();
		backAssessmentService.insertBackAssessment(back);
		//添加评估条件后再返回 刷新评估列表
		map.put("assess", backAssessmentService.getBackAssessment(back.getCid()));
		return map;
	}
	
	////删除一条记录的ajax请求
	@ResponseBody
	@RequestMapping("/deleteAssessByid")
	public boolean deleteAssessByid(Integer val_id){
		assessmentValueService.deleteAssessById(val_id);
		return true;
	}
	
	
	
	//添加一条记录
	@ResponseBody
	@RequestMapping("/addAessess")
	public Map<String, Object>insertAssess(Integer assess_id,Integer back_id,double assess_price,String assess_value){
		AssessmentValue as = new AssessmentValue();
		as.setBack_id(back_id);
		as.setAssess_id(assess_id);
		as.setAssess_price(assess_price);
		as.setAssess_value(assess_value);
		assessmentValueService.insertAssessmentValue(as);
		System.out.println("-------:"+as);
		map.put("as", as);
		return map;
	}
	
	
	
	//编辑
		@ResponseBody
		@RequestMapping("/editAssess")
		public boolean editAssess(Integer val_id,double assess_price,String assess_value){
			AssessmentValue as = new AssessmentValue();
			as.setVal_id(val_id);
			as.setAssess_price(assess_price);
			as.setAssess_value(assess_value);
			assessmentValueService.editAssess(as);
			map.put("as", as);
			return true;
		}
		
	
		@RequestMapping("/getasinfo")
		public String getasinfo(Integer cid,HttpServletRequest req){
			
	   req.setAttribute("bas",backAssessmentService.getBackAssessment(cid));
			
			return "assess_info";
		}
		
		
		@ResponseBody
		@RequestMapping("/checkAssessExsit")
		public boolean checkAssessExsit(Integer assess_id){
			AssessmentValue as = new AssessmentValue();
			
			boolean rs =true;
			as.setAssess_id(assess_id);
		    List<AssessmentValue> assessmentVlaues =assessmentValueService.getAssessmentValue(as);
			if(assessmentVlaues!=null&&assessmentVlaues.size()>0) {
				rs =false;
			}
			return rs;
		}
		
		
		@ResponseBody
		@RequestMapping("/deleteAssess")
		public boolean deleteAssess(Integer assess_id){
			AssessmentValue as = new AssessmentValue();
			boolean rs =true;
			backAssessmentService.deleteBackAssessment(assess_id);
			
			return rs;
		}
		
		
		
		
}
